package com.jing.weibo.controller.user;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import weibo4j.model.WeiboException;

import com.jing.system.comm.BaseController;
import com.jing.system.comm.contants.WeiboCons;
import com.jing.system.comm.enums.RuleType;
import com.jing.system.utils.DateUtil;
import com.jing.system.utils.PointCurrencyUtil;
import com.jing.weibo.pojo.user.UserInfo;
import com.jing.weibo.pojo.user.UserSign;
import com.jing.weibo.service.user.UserInfoService;
import com.jing.weibo.service.user.UserSignService;

/**
 * 用户签到记录的Controller
 * @author jing.yue
 * @version 2012/09/29 1.0.0
 */
@Controller
public class SignController extends BaseController {

	private static final Logger logger = Logger.getLogger(SignController.class);

	@Autowired
	private UserSignService userSignService;
	@Autowired
	private UserInfoService userInfoService;

	/**
	 * 添加用户签到记录信息
	 * @return
	 */
	@RequestMapping(value = "/sign/json/addUserSign")
	@ResponseBody
	public ModelMap addUserSign(HttpServletRequest request, UserSign userSign) {
		String result = null;
		UserInfo userInfo = getSessionUser(request);
		StringBuffer errorWeiboMsg = new StringBuffer();

		try {
			if(userInfo == null) {
				result = ERROR_NOT_LOGIN;
			} else {
				//判断今天是否签过到了
				userSign.setStringtime(DateUtil.dateToString(DateUtil.getTime(), DateUtil.FMT_YYYY_MM_DD));
				userSign.setUsername(userInfo.getUsername());
				if(userSignService.findByUsernameTodaySign(userSign) == null) {
					userSignService.saveUserSign(userSign);

					//每日签到积分处理
					PointCurrencyUtil pointCurrencyUtil = new PointCurrencyUtil(RuleType.TYPE_EVERY_SIGN_IN, userInfo.getUsername());
					pointCurrencyUtil.handlePointCurrency();
					//同步Session中的用户信息
					setSessionUser(request, userInfoService.findByUserName(userInfo.getUsername()));

					//发送签到信息到微博
					//sinaService.updateStatus(SystemCons.getUserSignContent(RuleType.TYPE_EVERY_SIGN_IN), userInfo.getToken());

					result = SUCCESS;
				}
				else {
					result = "user_sign_yes_today";
				}
			}
		} catch (WeiboException e) {
			logger.error(WeiboCons.getErrorMsg(e));
			result = ERROR_WEIBO;
			errorWeiboMsg.append("调用微博接口出现异常[").append(WeiboCons.getErrorMsg(e)).append("]!");
		} catch (Exception e) {
			result = ERROR;
			logger.error(userInfo.getNickname() + ": 添加用户签到记录信息异常!", e);
		}
		ModelMap modelMap = new ModelMap();
		modelMap.put("errorWeiboMsg", errorWeiboMsg.toString());
		modelMap.put("result", result);
		return modelMap;
	}

	/**
	 * 根据用户名获取用户签到记录信息
	 * @return
	 */
	@RequestMapping(value = "/sign/json/findByUsernameSign")
	@ResponseBody
	public ModelMap findByUsernameSign(HttpServletRequest request, UserSign userSign) {
		String result = null;
		Map<String, Object> map = null;
		UserInfo userInfo = getSessionUser(request);
		try {
			userSign.setUsername(userInfo.getUsername());
			map = userSignService.findByUsernameSign(userSign);
			result = SUCCESS;
		} catch (Exception e) {
			logger.error(userInfo.getNickname() + ": 根据用户名获取用户签到记录信息异常!", e);
			result = ERROR;
		}
		ModelMap modelMap = new ModelMap();
		modelMap.put("data", map);
		modelMap.put("result", result);
		return modelMap;
	}
}